package android.theHead;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class theHead extends Activity {

	// global variable that stores the context of this Activity
	// used for function calls in nested methods
	Activity context = this;

	@Override 
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);		 


		// starting the background service
		Log.i("theHead","Starting background service");
		this.startService(new Intent(this, BackgroundService.class));

		// make an instance of the teamDBAdatpter to update the team info if necessary 
		TeamDBAdapter teamDB = new TeamDBAdapter(this);
		teamDB.open();
		Log.i("theHead","updating team info");
		teamDB.updateTeamInfo();
		teamDB.close();


		setContentView(R.layout.main);
		// find the specified button and define a inline onClick listener
		((Button)findViewById(R.id.pat_debug)).setOnClickListener(new View.OnClickListener(){
			public void onClick(View v){
				// create an explicit intent to call the patDebug activity
				Intent anIntent = new Intent(context, patDebug.class);
				// start activity with the Intent
				context.startActivity(anIntent);
			}
		});

		// overview button
		((Button)findViewById(R.id.MainListButton)).setOnClickListener(new View.OnClickListener(){

			public void onClick(View v){
				// create an explicit intent to call the patDebug activity
				Intent anIntent = new Intent(context, Overview.class);
				// start activity with the Intent
				context.startActivity(anIntent);
			}
		});

		// Team button
		((Button)findViewById(R.id.MainTeamButton)).setOnClickListener(new View.OnClickListener(){
			public void onClick(View v){
				// create an explicit intent to call the patDebug activity
				Intent anIntent = new Intent(context, TeamView.class);
				// start activity with the Intent
				context.startActivity(anIntent);
			}
		});
		
		// Data button
		((Button)findViewById(R.id.MainDataButton)).setOnClickListener(new View.OnClickListener(){
			public void onClick(View v){
				// create an explicit intent to call the patDebug activity
				Intent anIntent = new Intent(context, TeamData.class);
				// start activity with the Intent
				context.startActivity(anIntent);
			}
		});
	}
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main_menu, menu);
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {

		Intent anIntent;

		// Handle item selection	    	
		switch (item.getItemId()) {
		case R.id.mode_rower:
			anIntent = new Intent(context, RowmodeIntro.class);
			context.startActivity(anIntent);
			
			return true;
		case R.id.gotoMapView:
			anIntent = new Intent(context, ViewMap.class);
			context.startActivity(anIntent);

			return true;
//		case R.id.gotoListView:
//			anIntent = new Intent(context, Overview.class);
//			context.startActivity(anIntent);
//
//			return true;
//		case R.id.gotoDataView:
//			anIntent = new Intent(context, TeamData.class);
////			anIntent.putExtra("teamID", selection);
//			context.startActivity(anIntent);
//
//			return true;
//		case R.id.gotoTeamView:
//			anIntent = new Intent(context, TeamView.class);
////			anIntent.putExtra("teamID", selection);
//			context.startActivity(anIntent);
//
//			return true;
		case R.id.quit:
			finish();
		default:
			return false;
		}
	}


	public void onPause(){
		super.onPause();
		Log.i("theHead","main activity pauses");
	}

	public void onStop(){
		super.onStop();
		Log.i("theHead","main activity stops");
	}

	public void onDestroy(){
		super.onDestroy();
		Log.i("theHead","main activity destroys");

		// stop the background service
		Log.i("theHead","stopping background service");
		this.stopService(new Intent(this, BackgroundService.class));
	}


}